function modaldemo(id) {
    $.post("/manage/ajax_select", {
        id: id
    }, function(result) {
        $("#company").html(result.company);
        $("#select").html(result.select);
        if (id) {
            $(".modal-title").html("编辑账号");
            $("#id").val(result.info.id);
            $("input[name='user_name']").val(result.info.user_name);
            $("input[name='user_name']").attr("disabled", true);
            $("input[name='nickname']").val(result.info.nickname);
            $("input[name='password']").attr("placeholder", "为空表示不修改密码")
        } else {
            $("input[name='user_name']").val('');
            $("input[name='nickname']").val('');
            $("#id").val('');
            $(".modal-title").html("添加账号");
            $("input[name='user_name']").attr("disabled", false);
            $("input[name='password']").attr("placeholder", "请输入登录密码")
        }
    });
    $("#modal-demo").modal("show")
}
//根据公司获取职位
function getPosition(){
    var cid = $("#cid").val();
    $.get("/manage/getPosition", {
        cid: cid
    }, function(result) {
        $("#select").html(result.position);
        
    });
}
//添加/修改
$(".btn-account").click(function() {
    var user_name = $("input[name='user_name']").val();
    var password = $("input[name='password']").val();
    var nickname = $("input[name='nickname']").val();
    var id = $("#id").val();
    var cid = $("#cid option:selected ").val();
    var role_id = $("#role_id option:selected").val();
    if (user_name == '') {
        parent.layer.msg("账号不能为空");
        return false
    }
    if (!id) {
        if (password == '') {
            parent.layer.msg("密码不能为空");
            return false
        }
    }
    if (nickname == '') {
        parent.layer.msg("昵称不能为空");
        return false
    }
    $.post("/manage/editAccount", {
        name: user_name,
        pwd: password,
        role_id: role_id,
        nickname:nickname,
        id: id,
        cid:cid
    }, function(result) {
        parent.layer.msg(result['msg']);
        if (result['code'] == 1) {
            $("#modal-demo").modal("hide");
            setTimeout(function() {
                window.location.reload();
            }, 1000)
        }
    })
})
//删除
$(".bt-acc-del").click(function(){
    var id = $(this).attr("data-id");
    parent.layer.confirm('确定删除账号吗', {
        btn: ['删除','取消'], //按钮
        shade: false //不显示遮罩
    }, function(){
        $.post("/manage/delAccount",{id:id},function(result){
            parent.layer.msg(result['msg']);
            if(result['code'] == 1){
                setTimeout(function() {
                    window.location.reload();
                }, 1000)
            } 
        });
    }, function(){
        parent.layer.msg('操作取消');
    });   
})
